package com.xinke.module.system.controller.admin.notice;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.notice.*;
import com.xinke.module.system.model.entity.notice.NotifyTemplateDO;
import com.xinke.module.system.service.notice.NotifyTemplateService;

@Tag(name = "管理后台 - 站内信模板")
@RestController
@RequestMapping("/system/notify-template")
@Validated
public class NotifyTemplateController {

    @Resource
    private NotifyTemplateService notifyTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建站内信模板")
    @PreAuthorize("@ss.hasPermission('system:notify-template:create')")
    public Result<Long> createNotifyTemplate(@Valid @RequestBody NotifyTemplateSaveReqVO createReqVO) {
        return success(notifyTemplateService.createNotifyTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新站内信模板")
    @PreAuthorize("@ss.hasPermission('system:notify-template:update')")
    public Result<Boolean> updateNotifyTemplate(@Valid @RequestBody NotifyTemplateSaveReqVO updateReqVO) {
        notifyTemplateService.updateNotifyTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除站内信模板")
    @Parameter(name = "templateId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:notify-template:delete')")
    public Result<Boolean> deleteNotifyTemplate(@RequestParam("templateId") Long templateId) {
        notifyTemplateService.deleteNotifyTemplate(templateId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得站内信模板")
    @Parameter(name = "templateId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:notify-template:query')")
    public Result<NotifyTemplateRespVO> getNotifyTemplate(@RequestParam("templateId") Long templateId) {
        NotifyTemplateDO notifyTemplate = notifyTemplateService.getNotifyTemplate(templateId);
        return success(BeanUtils.toBean(notifyTemplate, NotifyTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得站内信模板分页")
    @PreAuthorize("@ss.hasPermission('system:notify-template:query')")
    public Result<PageResult<NotifyTemplateRespVO>> getNotifyTemplatePage(@Valid NotifyTemplatePageReqVO pageReqVO) {
        PageResult<NotifyTemplateDO> pageResult = notifyTemplateService.getNotifyTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, NotifyTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出站内信模板 Excel")
    @PreAuthorize("@ss.hasPermission('system:notify-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportNotifyTemplateExcel(@Valid NotifyTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<NotifyTemplateDO> list = notifyTemplateService.getNotifyTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "站内信模板.xls", "数据", NotifyTemplateRespVO.class,
                        BeanUtils.toBean(list, NotifyTemplateRespVO.class));
    }

}